package com.yanftch.jsonformat

import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.ui.DialogBuilder
import com.yanftch.jsonformat.json2ets.generator.GeneratorDelegate
import com.yanftch.jsonformat.json2ets.parser.typescript.ParseType
import com.yanftch.jsonformat.json2ets.views.Json2TsForm
import com.yanftch.jsonformat.json2ets.views.SetGetChooseDialog

class Json2etsAction : AnAction() {
    private val generatorDelegate = GeneratorDelegate()

    override fun actionPerformed(event: AnActionEvent) {
        DialogBuilder().apply {
            val form = Json2TsForm().apply {
                setOnGenerateListener(object : Json2TsForm.OnGenerateClicked {
                    override fun onClicked(rootName: String, json: String, parseType: ParseType) {
                        var generateSetGet = false
                        if (ParseType.TSClass == parseType) {
                            val dialog = SetGetChooseDialog()
                            dialog.setOnButtonClickListener { isConfirm ->
                                // 生成 set()/get() 方法
                                generateSetGet = isConfirm
                            }
                            dialog.isVisible = true
                        } else {
                            generateSetGet = false
                        }
                        window.dispose()
                        generatorDelegate.runGeneration(event, json, rootName, parseType, generateSetGet)
                    }
                })
            }
            setCenterPanel(form.rootView)
            setTitle("Json2ets")
            removeAllActions()
            show()
        }
    }

    override fun update(e: AnActionEvent) {
        e.presentation.isEnabled = true
    }
}
